/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.daistatistic;

import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import robocup.component.RobocupAgent;
import soccerscope.SoccerScopeMenuBar;
import soccerscope.model.ClientScene;
import soccerscope.model.WorldModel;

/**
 * 
 * implements the dialog for the player data
 */
public class PlayerDataDialog implements WindowListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3617858564509544504L;

	private static int dialogWidth = 300; // dialog dimensions

	private static int dialogHigh = 300;

	private static Dialog dialog; // reference to a Dialog

	private static JTable table; // reference a new JTable

	private static MyTableModel model; // reference a new Table Model

	private static JProgressBar staminaProgressBar; // reference two progress

	// bars

	private static JProgressBar speedProgressBar;

	protected static int selectedPlayerNumber = 1; // show data of the player

	// with that number

	private static boolean beep = false;

	Choice select1; // reference a now drop down menu

	/**
	 * @author Andr�
	 * 
	 * this class defines the Player Data Dialog
	 */
	public PlayerDataDialog(SoccerScopeMenuBar bar,
			soccerscope.SoccerScope frame, int yPixelMore) {

		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		int x = frame.getX() + frame.getWidth();
		while (x > (ge.getMaximumWindowBounds().width - dialogWidth)) {
			x--;
		}
		dialog = new Dialog(frame, "Player Data"); // create a new dialog with
		// our scope as "parent"
		dialog.setLayout(new FlowLayout(FlowLayout.CENTER));

		dialog.setSize(dialogWidth, dialogHigh); // set size
		dialog.setLocation(x, frame.getY() + yPixelMore);
		dialog.addWindowListener(this);
		dialog.setBackground(Color.LIGHT_GRAY);
		dialog.setVisible(false);

		// fill the drop down menu
		this.select1 = new Choice();
		this.select1.addItemListener(new MyItemListener());
		this.select1.addItem("Player 1");
		this.select1.addItem("Player 2");
		this.select1.addItem("Player 3");
		this.select1.addItem("Player 4");
		this.select1.addItem("Player 5");
		this.select1.addItem("Player 6");
		this.select1.addItem("Player 7");
		this.select1.addItem("Player 8");
		this.select1.addItem("Player 9");
		this.select1.addItem("Player 10");
		this.select1.addItem("Player 11");

		dialog.add(this.select1);

		// create the model which contains the data for the table
		model = new MyTableModel();

		// create the table
		table = new JTable();
		table.setRowSelectionAllowed(false);
		table.setColumnSelectionAllowed(false);
		table.setModel(model);
		dialog.add(table);

		// create the progress bars

		Panel progressPanel = new Panel();
		progressPanel.setLayout(new BorderLayout());
		staminaProgressBar = new JProgressBar(0, 4000);
		staminaProgressBar.setBackground(Color.BLACK);
		staminaProgressBar.setForeground(Color.BLUE);
		staminaProgressBar.setPreferredSize(new Dimension(90, 20));
		staminaProgressBar.setStringPainted(true);
		staminaProgressBar.setString("Stamina");
		progressPanel.add("West", staminaProgressBar);

		speedProgressBar = new JProgressBar(0, 120);
		speedProgressBar.setBackground(Color.BLACK);
		speedProgressBar.setForeground(Color.BLUE);
		speedProgressBar.setPreferredSize(new Dimension(90, 20));
		speedProgressBar.setStringPainted(true);
		speedProgressBar.setString("Speed");
		progressPanel.add("East", speedProgressBar);

		progressPanel.setPreferredSize(new Dimension(200, 20));
		dialog.add(progressPanel);

		setPlayer(1);

		// start thread for refill the table
		Controll controll = new Controll();
		Thread th = new Thread(controll);
		th.start();
	}

	/**
	 * 
	 * returns true if the dialog is visible else false
	 * 
	 * @return boolean
	 */
	public boolean dialogVisible() {
		return dialog.isVisible();
	}

	/**
	 * 
	 * set visible or not
	 * 
	 * @param boolean
	 */
	public static void setVisible(boolean mode) {
		dialog.setVisible(mode);
	}

	/**
	 * 
	 * set to a new player, also the actual player in action dialog will set to
	 * that player
	 * 
	 * @param int
	 *            playerNumber, the new player
	 */
	public static void setPlayer(int playerNumber) {
		selectedPlayerNumber = playerNumber;
		RobocupAgent player = null; // RobocupAgent.createAgent(new
		// Properties());
		if (playerNumber > 0 && playerNumber < 12) {
			if (ActionDialog.getDialog() != null) {
				ActionDialog.actualPlayerNumber = playerNumber;
				ActionDialog.setTitle("Action Dialog for Player "
						+ playerNumber);
				ActionDialog.buttonsOff();
			}
			player = Register.getPlayerBeanWithPlayerNumber(playerNumber);
		}
		if (player != null)
			fillTable(player);
	}

	/**
	 * 
	 * fills the table model with actual data and repaint the table
	 * 
	 * @param PlayerBean
	 *            player the player
	 */
	public static void fillTable(RobocupAgent player) {
		if (player == null) {
			if (!beep)
				Toolkit.getDefaultToolkit().beep();
			beep = true;
			return;
		}
		beep = false;
		model.setBallPosition(player.getWorld().getBallRef().getPosition().x,
				player.getWorld().getBallRef().getPosition().y);
		model.setPos(player.getWorld().getSelfRef().getPosition().x, player
				.getWorld().getSelfRef().getPosition().y);
		model.setSpeedXY(player.getWorld().getSelfRef().getSpeed().x, player
				.getWorld().getSelfRef().getSpeed().y);
		model.setStamina(player.getWorld().getStamina());
		model.setAngles(player.getWorld().getViewWidth(), player.getWorld()
				.getSelfRef().getNeckAngle(), player.getWorld().getBodyAngle());

		ClientScene scene = WorldModel.sceneSet.lastScene();
		model.setCoachPos(
				scene.player[player.getWorld().getNumber() - 1].pos.y,
				scene.player[player.getWorld().getNumber() - 1].pos.x);
		model.setCoachBallPosition(scene.ball.pos.y, scene.ball.pos.x);

		table.repaint();

	}

	/**
	 * 
	 * repaint the table
	 */
	public static void tableRepaint() {
		fillTable(Register.getPlayerBeanWithPlayerNumber(selectedPlayerNumber));
		table.repaint();
	}

	/**
	 * 
	 * set the value for the stamina progress bar
	 * 
	 * @param int
	 *            value
	 */
	public static void setStaminaProgressBarValue(int value) {
		staminaProgressBar.setValue(value);
	}

	/**
	 * 
	 * set the value for the speed progress bar
	 * 
	 * @param double
	 *            value
	 */
	public static void setSpeedProgressBarValue(double value) {
		String help = Double.toString(value);
		if (help.indexOf(".") > 0)
			help = help.substring(0, help.indexOf("."));
		speedProgressBar.setValue(Integer.parseInt(help));
	}

	/**
	 * 
	 * getter for the dialog
	 */
	public static Dialog getDialog() {
		return dialog;
	}

	public void windowOpened(WindowEvent arg0) {
	}

	public void windowClosing(WindowEvent arg0) {
		dialog.setVisible(false);
	}

	public void windowClosed(WindowEvent arg0) {
	}

	public void windowIconified(WindowEvent arg0) {
	}

	public void windowDeiconified(WindowEvent arg0) {
	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowDeactivated(WindowEvent arg0) {
	}

}

/**
 * 
 * the item listener for the pull down menu
 */
class MyItemListener implements ItemListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	public void itemStateChanged(ItemEvent arg0) {
		Choice target = (Choice) arg0.getSource();

		if (arg0.getID() == Event.LIST_SELECT
				|| arg0.getID() == Event.LIST_DESELECT
				|| arg0.getID() == Event.SCROLL_END) {

			if (target.getSelectedItem().equals("Player 1")) {
				PlayerDataDialog.setPlayer(1);
			}
			if (target.getSelectedItem().equals("Player 2")) {
				PlayerDataDialog.setPlayer(2);
			}
			if (target.getSelectedItem().equals("Player 3")) {
				PlayerDataDialog.setPlayer(3);
			}
			if (target.getSelectedItem().equals("Player 4")) {
				PlayerDataDialog.setPlayer(4);
			}
			if (target.getSelectedItem().equals("Player 5")) {
				PlayerDataDialog.setPlayer(5);
			}
			if (target.getSelectedItem().equals("Player 6")) {
				PlayerDataDialog.setPlayer(6);
			}
			if (target.getSelectedItem().equals("Player 7")) {
				PlayerDataDialog.setPlayer(7);
			}
			if (target.getSelectedItem().equals("Player 8")) {
				PlayerDataDialog.setPlayer(8);
			}
			if (target.getSelectedItem().equals("Player 9")) {
				PlayerDataDialog.setPlayer(9);
			}
			if (target.getSelectedItem().equals("Player 10")) {
				PlayerDataDialog.setPlayer(10);
			}
			if (target.getSelectedItem().equals("Player 11")) {
				PlayerDataDialog.setPlayer(11);
			}
		}
	}
}

/**
 * 
 * the TableModel
 */
class MyTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3905805279462373683L;

	// Player Values
	String xPos = "0";

	String yPos = "0";

	String stamina = "3000";

	String speed = "0.0";

	String speedX = "0.0";

	String speedY = "0.0";

	String viewQuality = "1";

	String viewAngle = "90";

	String neckAngle = "0";

	String bodyAngle = "0";

	String ballPosition = "(0,0)";

	String holes = "0";

	String actions = "0";

	String noActions = "0";

	// Coach Values
	String cxPos = "0";

	String cyPos = "0";

	String cballPosition = "(0,0)";

	public int getRowCount() {
		return 12;
	}

	public int getColumnCount() {
		return 3;
	}

	/**
	 * 
	 * get the object of a spezified position
	 * 
	 * @param int
	 *            row
	 * @param int
	 *            col
	 */
	public Object getValueAt(int row, int col) {
		if (col == 0) {
			if (row == 0)
				return " Position X";
			if (row == 1)
				return " Position Y";
			if (row == 2)
				return " Stamina";
			if (row == 3)
				return " Speed";
			if (row == 4)
				return " View Quality";
			if (row == 5)
				return " View Angle";
			if (row == 6)
				return " Neck Angle";
			if (row == 7)
				return " Body Angle";
			if (row == 8)
				return " Ball Position";
			if (row == 9)
				return " Holes";
			if (row == 10)
				return " Actions";
			if (row == 11)
				return " No Actions";
		}

		if (col == 1) {
			if (row == 0)
				return this.xPos;
			if (row == 1)
				return this.yPos;
			if (row == 2) {

				PlayerDataDialog.setStaminaProgressBarValue(Integer
						.parseInt(this.stamina));
				return this.stamina;
			}
			if (row == 3) {
				this.speed = Double.toString(java.lang.Math.sqrt(Double
						.parseDouble(this.speedX)
						* Double.parseDouble(this.speedX)
						+ Double.parseDouble(this.speedY)
						* Double.parseDouble(this.speedY)));
				PlayerDataDialog.setSpeedProgressBarValue(Double
						.parseDouble(this.speed) * 100);
				return "(" + this.speedX + "," + this.speedY + ")";
			}
			if (row == 4)
				return this.viewQuality;
			if (row == 5)
				return this.viewAngle;
			if (row == 6)
				return this.neckAngle;
			if (row == 7)
				return this.bodyAngle;
			if (row == 8)
				return this.ballPosition;
			if (row == 9)
				return this.holes;
			if (row == 10)
				return this.actions;
			if (row == 11)
				return this.noActions;
		}
		if (col == 2) {
			if (row == 0)
				return this.cxPos;
			if (row == 1)
				return this.cyPos;
			if (row == 8)
				return this.cballPosition;
		}

		return "";
	}

	/**
	 * 
	 * set the absolute (coach view) position
	 * 
	 * @param double
	 *            x
	 * @param double
	 *            y
	 */
	public void setCoachPos(double x, double y) {
		this.cxPos = Double.toString(x);
		this.cyPos = Double.toString(y);
		if (this.cxPos.length() > 5)
			this.cxPos = this.cxPos.substring(0, 5);
		if (this.cyPos.length() > 5)
			this.cyPos = this.cyPos.substring(0, 5);
	}

	/**
	 * 
	 * set the absolute (coach view) ball position
	 * 
	 * @param double
	 *            x
	 * @param double
	 *            y
	 */
	public void setCoachBallPosition(double x, double y) {
		String helpX = Double.toString(x);
		String helpY = Double.toString(y);
		if (helpX.length() > 5)
			helpX = helpX.substring(0, 5);
		if (helpY.length() > 5)
			helpY = helpY.substring(0, 5);
		this.cballPosition = "(" + helpX + "," + helpY + ")";
	}

	/**
	 * 
	 * set the position
	 * 
	 * @param double
	 *            x
	 * @param double
	 *            y
	 */
	public void setPos(double x, double y) {
		this.xPos = Double.toString(x);
		this.yPos = Double.toString(y);
		if (this.xPos.length() > 5)
			this.xPos = this.xPos.substring(0, 5);
		if (this.yPos.length() > 5)
			this.yPos = this.yPos.substring(0, 5);
		// System.out.println("XPos: " + xPos + " YPos: " + yPos);
	}

	/**
	 * 
	 * set the stamina
	 * 
	 * @param double
	 *            stam
	 */
	public void setStamina(double stam) {
		String help = Double.toString(stam);
		if (help.indexOf(".") > 0)
			help = help.substring(0, help.indexOf("."));
		this.stamina = help.toString();
	}

	/**
	 * 
	 * set the speed
	 * 
	 * @param double
	 *            sp
	 */
	public void setSpeed(double sp) {
		this.speed = Double.toString(sp);
	}

	/**
	 * 
	 * set the speed with x and y direction
	 * 
	 * @param double
	 *            x
	 * @param double
	 *            y
	 */
	public void setSpeedXY(double x, double y) {
		this.speedX = Double.toString(x);
		this.speedY = Double.toString(y);
		if (this.speedX.length() > 5)
			this.speedX = this.speedX.substring(0, 5);
		if (this.speedY.length() > 5)
			this.speedY = this.speedY.substring(0, 5);
	}

	/**
	 * 
	 * set the view quality
	 * 
	 * @param double
	 *            qual
	 */
	public void setViewQuality(double qual) {
		this.viewQuality = Double.toString(qual);
	}

	/**
	 * 
	 * set the player angles
	 * 
	 * @param double
	 *            view
	 * @param double
	 *            neck
	 * @param double
	 *            body
	 */
	public void setAngles(double view, double neck, double body) {
		this.viewAngle = Double.toString(view);
		if (this.viewAngle.length() > 5)
			this.viewAngle = this.viewAngle.substring(0, 5);
		this.neckAngle = Double.toString(neck);
		if (this.neckAngle.length() > 5)
			this.neckAngle = this.neckAngle.substring(0, 5);
		this.bodyAngle = Double.toString(body);
		if (this.bodyAngle.length() > 5)
			this.bodyAngle = this.bodyAngle.substring(0, 5);
	}

	/**
	 * 
	 * set the ball position
	 * 
	 * @param double
	 *            x
	 * @param double
	 *            y
	 */
	public void setBallPosition(double x, double y) {
		String helpX = Double.toString(x);
		String helpY = Double.toString(y);
		if (helpX.length() > 5)
			helpX = helpX.substring(0, 5);
		if (helpY.length() > 5)
			helpY = helpY.substring(0, 5);
		this.ballPosition = "(" + helpX + "," + helpY + ")";
	}

	/**
	 * 
	 * set the holes
	 * 
	 * @param int
	 *            h
	 */
	public void setHoles(int h) {
		this.holes = Double.toString(h);
	}

	/**
	 * 
	 * set the executed actions
	 * 
	 * @param double
	 *            a
	 */
	public void setActions(double a) {
		this.actions = Double.toString(a);
	}

	/**
	 * 
	 * set the actions without holes
	 * 
	 * @param double
	 *            awh
	 */
	public void setActionsWithoutHoles(double awh) {
		this.noActions = Double.toString(awh);
	}

}

class Controll implements Runnable {

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	int cycle = 0;

	RobocupAgent player = null;

	public void run() {
		this.player = Register.getPlayerBeanWithPlayerNumber(1);
		if (this.player != null) {
			this.cycle = this.player.getWorld().getCycle();
			while (true) {
				if (this.player.getWorld().getCycle() != this.cycle) {
					PlayerDataDialog.tableRepaint();
					this.cycle++;
				}
				try {
					Thread.sleep(80);

				} catch (Exception e) {
				}
			}
		}
	}
}